﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Validation.sp2010.Classes;

namespace Validation.sp2010.Helper
{
   public class HelperParser
    {
       public static List<string> GetStringsFormula(string formule)
       {
           List<string> items = new List<string>();
           int cmp1 = 0;
           int cmp2 = 0;
           int cmp = 0;

           

           int length = formule.Length;
           while (cmp < length)
           {
               if (formule[cmp] == Constante.SurroundC1)
               {
                   cmp1 = cmp;
                   
               }
               else
               {
                   if (formule[cmp] == Constante.SurroundC2)
                   {
                       //closed
                       cmp2 = cmp - 1;
                       items.Add(formule.Substring(cmp1 + 1, cmp2 - cmp1));
                   }
                  
               }
               cmp++;
           }
               
           return items;
       }

       /// <summary>
       /// return finale string based on list Fields
       /// </summary>
       /// <param name="internaleFormule"></param>
       /// <returns></returns>
       public static string GetFinalStringFromList(SPList list,SPListItem item, string internaleFormule)
       {
           int cmp1 = 0;
           int cmp2 = 0;
           int cmp = 0;
           string finalString = "";
           bool surround = false;
           int length = internaleFormule.Length;
           while (cmp < length)
           {
               if (internaleFormule[cmp] == Constante.SurroundC1)
               {
                   cmp1 = cmp;
                   surround = true;

               }
               else
               {
                   if (internaleFormule[cmp] == Constante.SurroundC2)
                   {
                       //closed
                       cmp2 = cmp - 1;
                       string idFieldString = internaleFormule.Substring(cmp1 + 1, cmp2 - cmp1);
                       Guid idField = new Guid(idFieldString);
                       if (list.Fields.Contains(idField) && item[idField]!=null)
                       {
                           switch (list.Fields[idField].Type)
                           {

                               case SPFieldType.User:
                                   finalString += HelperUtilities.GetValueLookup(item[idField].ToString()); break;
                               default:
                                   finalString += item[idField].ToString();break;

                           }
                       }
                       surround = false;
                   }
                   else
                   {
                       if(!surround)
                       finalString += internaleFormule[cmp];
                   }

               }
               cmp++;
           }
           return finalString;

       }
       public static bool VerifyFormule(string formule)
       {
           int length = formule.Length;
           int cmp = 0;

           bool inSurround=false;
           while (cmp < length)
           {
               if (formule[cmp] == Constante.SurroundC1)
               {
                   if(inSurround)
                       return false;

                   inSurround = true;
               }
               else
               {
                   if (formule[cmp] == Constante.SurroundC2)
                   {
                       if (!inSurround)
                           return false;

                       inSurround = false;
                   }
               }
               cmp++;
           }
           return !inSurround;
       }
    }
}
